﻿namespace CN2P.Web.Controllers.Api
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    using CN2P.ServiceModel;

    public class RecommendationsApiController : BaseApiController
    {
        private static readonly object Sync = new object();

        private static readonly List<RecommendationItemModel> Recommendations = new List<RecommendationItemModel>();

        static RecommendationsApiController()
        {
            //Recommendations.Add(new RecommendationItemModel("101", "10002"));
            //Recommendations.Add(new RecommendationItemModel("91201", "10003"));
        }

        [AcceptVerbs("GET")]
        public IList<string> Get([FromUri]RecommendationRequestModel request)
        {
            request = request ?? new RecommendationRequestModel();
            lock (Sync)
            {
                var items = Recommendations
                                             .SelectMany(order => order.GetProducts())
                                             .ToList()
                                             .Where(otherId => request.prodId.All(reqId => reqId != otherId))
                                             .Distinct()
                                             .ToList();
                return items;
            }
        }

        [AcceptVerbs("POST")]
        public HttpResponseMessage AddTraversal(RecommendationItemModel request)
        {
            lock (Sync)
            {
                Recommendations.Add(request);
            }

            return this.Request.CreateResponse(HttpStatusCode.Created);
        }

        [AcceptVerbs("DELETE")]
        public HttpResponseMessage DeleteAll()
        {
            lock (Sync)
            {
                Recommendations.Clear();
            }

            return this.Request.CreateResponse(HttpStatusCode.Created);
        }
    }
}
